using System.BusinessObjects.With;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.IO;
using Blazing.BusinessObjects.Assets;
using Blazing.BusinessObjects.Queries;
using Blazing.BusinessObjects.Tagging;

namespace Blazing.WebFacade
{
    [System.ComponentModel.DataObject]
    public class AssetController
    {

        #region Asset

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void DeleteAsset(Asset asset)
        {
            asset.Delete();
            asset.Save();
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public Asset NewAsset(Asset asset)
        {
            With.Transaction.Execute(() => asset.Save());
            return asset;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public Asset UpdateAsset(Asset asset)
        {
            Asset.Evict(asset.ID);
            Asset update = Asset.Load(asset.ID);

            update.Name = asset.Name;
            update.AssetType = asset.AssetType;
            update.Description = asset.Description;
            update.FileName = asset.FileName;
            update.FilePath = asset.FilePath;
            if (asset.TaxonomyTags.Count > 0)
                update.TaxonomyTags = asset.TaxonomyTags;

            With.Transaction.Execute(()=> update.Save());
            return update;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public IList<Asset> GetAssets()
        {
            return Asset.Search(QrySearchAssets.Query());
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select)]
        public IList<Asset> GetAssets(int taxonomyTagId)
        {
            return Asset.Search(QrySearchAssets.Query(taxonomyTagId));
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select)]
        public IList<Asset> GetAssets(string path)
        {
            return Asset.Search(QrySearchAssets.Query(path));
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select)]
        public IList<Asset> GetAssetsByTags(TaxonomyTag taxonomyTag)
        {
            TaggingController tagCon = new TaggingController();
            List<TaxonomyTag> tags = new List<TaxonomyTag>();
            if (taxonomyTag != null)
            {
                tags.Add(taxonomyTag);
                return Asset.Search(QrySearchAssetsByTaxonomyTags.Query(tags));
            }
            else
            {
                return null;
            }            
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select)]
        public IList<Asset> GetAssetsByTags(List<TaxonomyTag> tags)
        {
            if (tags != null && tags.Count > 0)
            {
                return Asset.Search(QrySearchAssetsByTaxonomyTags.Query(tags));
            }
            else
            {
                return null;
            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select)]
        public IList<Asset> GetAssetsByTags(List<TaxonomyTag> tags, int assetTypeID)
        {
            return Asset.Search(QrySearchAssetsByTaxonomyTags.Query(tags, assetTypeID));
        }

        public IList<Asset> GenerateAssetsFromFiles(string virtualPath)
        {
            IList<Asset> assets = Asset.Search(QrySearchAssets.Query(virtualPath));
            string path = HttpContext.Current.Server.MapPath(virtualPath);
            string[] files = Directory.GetFiles(path);

            foreach (string file in files)
            {
                if (CheckFileType(file))
                {
                    if (Asset.Fetch(QryFetchAssetByFullPath.Query(virtualPath, Path.GetFileName(file))) == null)
                    {
                        // Create new asset record
                        Asset newAsset = new Asset();
                        newAsset.Description = virtualPath + Path.GetFileName(file);
                        newAsset.FileName = Path.GetFileName(file);
                        newAsset.FilePath = virtualPath;
                        newAsset.Name = Path.GetFileName(file);
                        AssetType aType = GetAssetTypes()[0];
                        newAsset.AssetType = aType;

                        //Add to result list
                        assets.Add(NewAsset(newAsset));
                    }
                }
            }
            return assets;
        }

      
        public bool CheckFileType(string fileName)
        {
            string ext = Path.GetExtension(fileName);
            switch (ext.ToLower())
            {
                case ".gif":
                case ".png":
                case ".jpg":
                case ".jpeg":
                    return true;
                default:
                    return false;
            }
        }
#endregion

        #region AssetType

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void DeleteAssetType(AssetType asset)
        {
            asset.Delete();
            asset.Save();
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int NewAssetType(AssetType asset)
        {
            asset.Save();
            return asset.ID;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public AssetType UpdateAssetType(AssetType asset)
        {
            AssetType.Evict(asset.ID);
            AssetType update = AssetType.Load(asset.ID);
            With.Transaction.Execute(delegate
            {
                update.Name = asset.Name;
                update.Description = asset.Description;
                update.Save();
            });
            return update;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public IList<AssetType> GetAssetTypes()
        {
            return AssetType.Search(QrySearchAssetTypes.Query());
        }

        #endregion

    }
}
